package com.itheima.dao;

import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * ClassName:UserDao
 * Package:com.itheima.dao
 * Description:
 * Date:2020/3/11 16:45
 * User: snow
 */
@Repository
public interface UserInfoDao {

	@Select("select * from bs_user where userName = #{userName}")
	@Results({
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "userName", column = "userName"),
			@Result(property = "userCode", column = "userCode"),
			@Result(property = "email", column = "email"),
			@Result(property = "userPassword", column = "userPassword"),
			@Result(property = "phoneNum", column = "phoneNum"),
			@Result(property = "userStatus", column = "userStatus"),
			@Result(property = "roles", column = "id", javaType = java.util.List.class,
					many = @Many(select = "com.itheima.dao.RoleDao.findRoleByUserId"))
	})
	public UserInfo findByUserName(String userName) throws Exception;

	public List<UserInfo> findAll() throws Exception;

	public UserInfo findUserById(String userId) throws Exception;

	public void save(UserInfo userInfo) throws Exception;

	@Select("select * from bs_user where id = #{userId}")
	@Results({
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "userName", column = "userName"),
			@Result(property = "userCode", column = "userCode"),
			@Result(property = "email", column = "email"),
			@Result(property = "userPassword", column = "userPassword"),
			@Result(property = "phoneNum", column = "phoneNum"),
			@Result(property = "userStatus", column = "userStatus"),
			@Result(property = "roles", column = "id", javaType = java.util.List.class,
					many = @Many(select = "com.itheima.dao.RoleDao.findRoleByUserId"))
	})
	public UserInfo findById(String userId) throws Exception;

	/*	@Select("select * from role where id not in (select roleId from users_role where userId=#{id})")
		@Results({
				@Result(id = true, property = "id", column = "id"),
				@Result(property = "roleName", column = "role_name"),
				@Result(property = "roleDesc", column = "role_desc"),
		})*/
	public List<Role> findOtherRole(String id) throws Exception;

	@Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
	public void addRoleToUser(@Param("userId") String userId,@Param("roleId") String roleId) throws Exception;

	UserInfo findUserInfoById(String id);

	void freeze(String id);

	void open(String id);
}
